//**********************************************************
//    File Name: FrmCreateClientSchedule.cs
//    Author: Team Motif
//    Date Created : 04/13/2007
//    Current Owner: Team Motif 
//                                                                            
//    Purpose: 
//    To Search for the client and create an appointment
//    after finding the matching HCW based on his/her
//    availability & skills.  After found the match, it will
//    create then save the appointment in the database.
//	  Copyright 2007
//**********************************************************
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Library;

namespace Elderhealth
{
    public partial class FrmCreateClientSchedule : Form
    {
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Default construcotr for FrmCreateClientSchedule
        /// -------------------------------------------------------------------------------
        /// </summary>
        public FrmCreateClientSchedule()
        {
            InitializeComponent();
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Overloaded construcotr for FrmCreateClientSchedule
        /// -------------------------------------------------------------------------------
        /// </summary>
        public FrmCreateClientSchedule(long tmpClientID)
        {
            mClientID = tmpClientID;
            InitializeComponent();
        }
        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get called on the click of Cancel button
        /// and will close the current form
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get called when the Form is loaded
        /// As soon as the form is loaded various functions will get 
        /// called to fill in the data
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void FrmCreateClientSchedule_Load(object sender, EventArgs e)
        {
            DisplayClientNeedsAndWeeklyHours();
            saveSchedule.Enabled = false;
            DisplayClientName();
            DisplayTaskList();
            DisplayTimeSlots();
            DisplayClientRemainingHours();
        }

        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function talks to the AvailableTimeSlot class and fetches the availability data from the database
        /// for a particular client for the currently selected week.
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void DisplayTimeSlots()
        {
            availDetailsGrid.Rows.Clear();

            DateTime currentDate = new DateTime(weekPicker.Value.Year, weekPicker.Value.Month, weekPicker.Value.Day,0,0,0);
            
            DateTime nextDate = new DateTime();
            nextDate = currentDate.AddDays(7);

            ArrayList tmpSlotsArr = Library.AvailableTimeSlot.GetAvailDetailsForClient(mClientID, currentDate, nextDate);

            AvailableTimeSlot tmpTimeSlot;
            String duration, tmpStartTime, tmpEndTime, tmpAvailabilityDate;
            

            //Loop continue only when the function returns anything
            if (tmpSlotsArr.Count != 0)
            {
                for (int i = 0; i < tmpSlotsArr.Count; i++)
                {
                    tmpTimeSlot = (AvailableTimeSlot)tmpSlotsArr[i];

                    //Calculating the duration as EndHour - BeginHour + EndMin - BeginMin
                    int hourDifference, minDifference;
                    hourDifference = (tmpTimeSlot.GetEndDateTime().Hour) - (tmpTimeSlot.GetBeginDateTime().Hour);
                    minDifference = (tmpTimeSlot.GetEndDateTime().Minute) - (tmpTimeSlot.GetBeginDateTime().Minute);
                    
                    String paddedZero = "";
                    if (minDifference < 10)
                    {
                        paddedZero = "0";
                    }
                    duration = hourDifference.ToString() + " : " + paddedZero + minDifference.ToString();
                    
                    DateTime startTime = tmpTimeSlot.GetBeginDateTime();
                    DateTime endTime = tmpTimeSlot.GetEndDateTime();

                    DateTime noonTime = new DateTime(startTime.Year,startTime.Month,startTime.Day,12,0,0);

                    String noonIndicator;

                    if (startTime >= noonTime)
                    {
                        noonIndicator = " PM";    
                    }
                    else
                    {
                        noonIndicator = " AM";
                    }
                    paddedZero = "";

                    int startHour, startMinute, endHour, endMinute;

                    startHour = tmpTimeSlot.GetStartHour();
                    startMinute = tmpTimeSlot.GetStartMinute();

                    endHour = tmpTimeSlot.GetEndHour();
                    endMinute = tmpTimeSlot.GetEndMinute();

                    if (startMinute < 10)
                    {
                        paddedZero = "0";
                    }

                    if (startHour > 12)
                    {
                        startHour = startHour - 12;
                    }
                    tmpStartTime = startHour.ToString() + " : " + paddedZero + startMinute.ToString() + noonIndicator;

                    if (endTime >= noonTime)
                    {
                        noonIndicator = " PM";    
                    }
                    else
                    {
                        noonIndicator = " AM";    
                    }

                    paddedZero = "";

                    if (tmpTimeSlot.GetEndMinute() < 10)
                    {
                        paddedZero = "0";
                    }

                    if (endHour > 12)
                    {
                        endHour = endHour - 12;
                    }

                    tmpEndTime = endHour.ToString() + " : " + paddedZero + endMinute.ToString() + noonIndicator;
                    
                    tmpAvailabilityDate = tmpTimeSlot.GetBeginDate().ToString();

                    //Adding Data in the AvailDetailsGrid
                    availDetailsGrid.Rows.Add(0,tmpTimeSlot.GetDay(), tmpStartTime, tmpEndTime, duration, tmpAvailabilityDate);
                }

                //Continue only if there are any rows added in the datagridview
                if (availDetailsGrid.RowCount > 0)
                {
                    availDetailsGrid.Sort(AvailDate, ListSortDirection.Ascending);
                }
            }
        }
        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function talks to the Task class and get the data from the database
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void DisplayTaskList()
        {
            Task TaskDetail;
            ArrayList tmpTaskList = new ArrayList();
            tmpTaskList = Library.Task.GetAllTasksForClient(mClientID);

            int numberOfTasks = tmpTaskList.Count;
            for (int i = 0; i < numberOfTasks; i++)
            {
                TaskDetail = (Task)tmpTaskList[i];
                taskList.Items.Add(TaskDetail.GetTaskName());
            }
            if (numberOfTasks == 0)
            {
                taskList.Items.Add("No Tasks approved for client");
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get number of scheduled hours 
        /// and special needs information for a client
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void DisplayClientRemainingHours()
        {
            int Result, RemHours;

            //Reamianing hours starting for the currently selected date.
            DateTime currentDate = new DateTime(weekPicker.Value.Year, weekPicker.Value.Month, weekPicker.Value.Day, 0, 0, 0);

            DateTime nextDate = new DateTime();
            nextDate = currentDate.AddDays(7);

            String szDate = currentDate.Month.ToString() + "/" +
                            currentDate.Day.ToString() + "/" +
                            currentDate.Year.ToString();
            String szNextdate = nextDate.Month.ToString() + "/" +
                                nextDate.Day.ToString() + "/" +
                                nextDate.Year.ToString();

            Result = Library.Client.GetClientRemainingHours(mClientID, szDate, szNextdate);
            RemHours= int.Parse(approvedHrs.Text) - Result;
            remainingHours.Text = RemHours.ToString();
            
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get number of approved hours
        /// and special needs information for a client
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void DisplayClientNeedsAndWeeklyHours()
        {
            ArrayList myArrayList = new ArrayList();
            myArrayList = Library.Client.GetClientNeedsAndWeeklyHours(mClientID);
            int numberOfParameters = myArrayList.Count;
            if (numberOfParameters > 0)
            {
                specialNeeds.Text = myArrayList[0].ToString();
                approvedHrs.Text = myArrayList[1].ToString();
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function talks to the Client class and get the data from the database
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void DisplayClientName()
        {
            Library.Client tmpClient = new Library.Client();
            tmpClient = Library.Client.GetClientByID(mClientID);
            fNameLabel.Text = tmpClient.GetFirstName();
            lName.Text = tmpClient.GetLastName();
            middleInitial.Text = tmpClient.GetMiddleName();
            
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function searchs for matching HCW for an appointment
        /// based on availability & tasks
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void hcwSearchButton_Click(object sender, EventArgs e)
        {

            // Refreshing the search results box.
            homecareWorkerGridView.Rows.Clear();

            //Resetting the message display label - this is needed when an error is rectified and Search HCW is clicked a second time
            messageDisplayTextBox.Visible = false;
            messageDisplayTextBox.Text = " ";

            int lengthOfSelectedDate = selectedDate.Text.Length;

            //The count is being assigned to an intermediate variable to increase readability
            int numberOfTasksSelected = selectedTasksBox.Items.Count;

            if (lengthOfSelectedDate == 0)
            {
                //Display message asking the to user select a time
                messageDisplayTextBox.Text = "Please select the preferred time. Scheduling cannot proceed if preffered time is not specified.";
                messageDisplayTextBox.Visible = true;
            }
            else if (numberOfTasksSelected == 0) 
            {
                //Display message asking the user select atleast one task
                messageDisplayTextBox.Text = "Please select a task. Scheduling cannot proceed if a task is not selected.";
                messageDisplayTextBox.Visible = true;
            }
            else
            {
                DateTime selectedStartTime = GetSelectedStartDate();
                DateTime selectedEndTime = GetSelectedEndDate();

                AvailableTimeSlot preferredTime = new Library.AvailableTimeSlot();
                preferredTime.SetBeginDateTime(selectedStartTime);
                preferredTime.SetEndDateTime(selectedEndTime);
                ArrayList taskNameList = new ArrayList();

                for (int i = 0; i < numberOfTasksSelected; i++)
                {
                    taskNameList.Add(selectedTasksBox.Items[i]);
                }
                ArrayList taskObjectsList = new ArrayList();
                taskObjectsList = Task.GetTasksFromName(taskNameList);

                ArrayList homecareWorkers = HomeCareWorker.GetWorkerByMatchAvailAndTask(preferredTime, taskObjectsList);
            
                int numberOfHomecareWorkers = homecareWorkers.Count;
                if (numberOfHomecareWorkers == 0)
                {
                    //Display message saying no matching homecare workers found
                    messageDisplayTextBox.Text = "No matching homecare workers where found. Please change the Search Criteria and try again.";
                    messageDisplayTextBox.Visible = true;
                }
                else
                {
                    for (int i = 0; i < numberOfHomecareWorkers; i++)
                    {
                        HomeCareWorker nextHomecareWorker = homecareWorkers[i] as HomeCareWorker;
                        string homecareWorkerFirstName = nextHomecareWorker.GetFirstName();
                        String homecareWorkerMiddleName = nextHomecareWorker.GetMiddleName();
                        String homecareWorkerLastName = nextHomecareWorker.GetLastName();
                        String homecareWorkerName = "";
                        if (homecareWorkerFirstName != null)
                        {
                            homecareWorkerName = homecareWorkerName + homecareWorkerFirstName + " ";
                        }
                        if (homecareWorkerMiddleName != null)
                        {
                            homecareWorkerName = homecareWorkerName + homecareWorkerMiddleName + " ";
                        }
                        if (homecareWorkerLastName != null)
                        {
                            homecareWorkerName = homecareWorkerName + homecareWorkerLastName + " ";
                        }
                        String address1 = nextHomecareWorker.GetAddress1();
                        String address2 = nextHomecareWorker.GetAddress2();
                        String city = nextHomecareWorker.GetCity();
                        String state = nextHomecareWorker.GetState();
                        string homecareWorkerAddress = "";
                        if (address1 != null)
                        {
                            homecareWorkerAddress = homecareWorkerAddress + address1 + " ";
                        }
                        if (address2 != null)
                        {
                            homecareWorkerAddress = homecareWorkerAddress + address2 + " ";
                        }
                        if (city != null)
                        {
                            homecareWorkerAddress = homecareWorkerAddress + city + " ";
                        }
                        if (state != null)
                        {
                            homecareWorkerAddress = homecareWorkerAddress + state + " ";
                        }
                        long homecareWorkerID = nextHomecareWorker.GetUserID();
                        homecareWorkerGridView.Rows.Add(0, homecareWorkerID, homecareWorkerName, homecareWorkerAddress, "View");
                    }
                    hcwSearchDone = true;
                    LockTimeDetails();
                }
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function gets called when a search has been made for homecare workers
        /// based on some search criteria. This prevents the user from changing the search
        /// criteria after a search has been made.
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void LockTimeDetails()
        {
            messageDisplayTextBox.Visible = true;
            messageDisplayTextBox.Text = "If you want to change the search criteria please select 'Clear' and continue";
            startHourCombo.Enabled = false;
            startMinCombo.Enabled = false;
            strNoonIndicatorCombo.Enabled = false;
            endHourCombo.Enabled = false;
            endMinCombo.Enabled = false;
            endNoonIndicatorCombo.Enabled = false;

            availDetailsGrid.Columns[0].ReadOnly = true;
            weekPicker.Enabled = false;

            hcwSearchButton.Enabled = false;
        }


        /// <summary>
        /// -------------------------------------------------------------------------------
        /// When the User deselects a task from the task Grid this function will get
        /// called. It will sequentially move the selected element from Selected Taskcombobox
        /// to the Task group box 
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void RemoveOneTask_Click(object sender, EventArgs e)
        {
            if (hcwSearchDone == true)
            {
                messageDisplayTextBox.Visible = true;
                messageDisplayTextBox.Text = "Homecare Worker Search has been made on the selected search criteria. If you want to change the search criteria please select 'Clear' and continue";
            }
            else
            {
                if (selectedTasksBox.SelectedIndex != -1)
                {
                    taskList.Items.Add(selectedTasksBox.SelectedItem.ToString());
                    selectedTasksBox.Items.RemoveAt(selectedTasksBox.SelectedIndex);
                }
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// When the User selects >> from the task Grid then this function will get
        /// called. It will sequentially move all the selected element from Task group box 
        /// to the Selected Taskcombobox
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void SelectAllTask_Click(object sender, EventArgs e)
        {
            if (hcwSearchDone == true)
            {
                messageDisplayTextBox.Visible = true;
                messageDisplayTextBox.Text = "Homecare Worker Search has been made on the selected search criteria. If you want to change the search criteria please select 'Clear' and continue";
            }
            else
            {
                int iCnt = taskList.Items.Count;
                if (iCnt > 0)
                {
                    for (int i = 0; i < iCnt; i++)
                    {
                        selectedTasksBox.Items.Add(taskList.Items[i].ToString());
                    }
                    taskList.Items.Clear();
                }
            }

        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// When the User selects > from the task Grid then this function will get
        /// called. It will sequentially move the selected element from Task group box 
        /// to the Selected Taskcombobox
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void SelectOneTask_Click(object sender, EventArgs e)
        {
            if (hcwSearchDone == true)
            {
                messageDisplayTextBox.Visible = true;
                messageDisplayTextBox.Text = "Homecare Worker Search has been made on the selected search criteria. If you want to change the search criteria please select 'Clear' and continue";
            }
            else
            {
                if (taskList.SelectedIndex != -1)
                {
                    selectedTasksBox.Items.Add(taskList.SelectedItem.ToString());
                    taskList.Items.RemoveAt(taskList.SelectedIndex);
                }
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will be called specifically to create Select Button
        /// in the DataGridView
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void CreateSelectButton()
        {
            availDetailsGrid.Columns[0].Visible = false;
            // to add a new column with checkbox for "view/edit" dyanamically in datagridview
            DataGridViewCheckBoxColumn combocol = new DataGridViewCheckBoxColumn();
            combocol.DataPropertyName = "select";
            combocol.HeaderText = "select";
            combocol.Width = 70;
            combocol.DisplayIndex = 0;
            availDetailsGrid.Columns.Add(combocol);


            // Making all the columns in the gridview as readonly and just the last one 
            // as editable
            int DataGridColCount = availDetailsGrid.Rows.Count;
            for (int iCntr = 1; iCntr < DataGridColCount; iCntr++)
            {
                availDetailsGrid.Columns[iCntr].ReadOnly = true;
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get the selected details on click and
        /// preselect those values in the comboxes like Hour, Mins
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void AvailDetailsGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (hcwSearchDone == true)
            {
                messageDisplayTextBox.Visible = true;
                messageDisplayTextBox.Text = "Homecare Worker Search has been made on the selected search criteria. If you want to change the search criteria please select 'Clear' and continue";
            }
            else
            {

                int UserSelectedRow = availDetailsGrid.SelectedCells[0].RowIndex;

                for (int iCntr = 0; iCntr < availDetailsGrid.Rows.Count; iCntr++)
                {
                    if (iCntr != UserSelectedRow)
                    {
                        if (availDetailsGrid.Rows[iCntr].Cells[0].Displayed.Equals(true))
                        {
                            availDetailsGrid.Rows[iCntr].Cells[0].Value = false;
                        }
                    }
                }

                //Displaying value in the Hidden text box for SelectedDate
                String tempDate;

                tempDate = DateTime.Parse(availDetailsGrid.Rows[UserSelectedRow].Cells[5].Value.ToString()).Month + "/" +
                           DateTime.Parse(availDetailsGrid.Rows[UserSelectedRow].Cells[5].Value.ToString()).Day + "/" +
                           DateTime.Parse(availDetailsGrid.Rows[UserSelectedRow].Cells[5].Value.ToString()).Year;

                String szStartDate = availDetailsGrid.Rows[UserSelectedRow].Cells[2].Value.ToString();
                String szEndDate = availDetailsGrid.Rows[UserSelectedRow].Cells[3].Value.ToString();

                selectedDate.Text = tempDate;

                //Displaying data in the Start Hour and End Hour Comboboxes
                startHourCombo.SelectedItem = szStartDate.Substring(0, szStartDate.IndexOf(" : "));
                endHourCombo.SelectedItem = szEndDate.Substring(0, szEndDate.IndexOf(" : "));

                //Displaying data in the Start Min Combobox
                int len, start;
                len = szStartDate.Length;
                start = szStartDate.IndexOf(" : ") + 3;
                startMinCombo.SelectedItem = szStartDate.Substring(start, 2);

                //Displaying data in the str Noon Indicator Combobox
                start = szStartDate.IndexOf(" : ") + 6;
                strNoonIndicatorCombo.SelectedItem = szStartDate.Substring(start, 2);

                //Displaying data in the End Min Combobox
                len = szEndDate.Length;
                start = szEndDate.IndexOf(" : ") + 3;
                endMinCombo.SelectedItem = szEndDate.Substring(start, 2);

                //Displaying data in the end Noon Indicator Combobox
                start = szEndDate.IndexOf(" : ") + 6;
                endNoonIndicatorCombo.SelectedItem = szEndDate.Substring(start, 2);
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get the selected start Date, end hours, end mins and end secs 
        /// from different places on the form and will return a DateTime object
        /// -------------------------------------------------------------------------------
        /// </summary>
        private DateTime GetSelectedStartDate()
        {
            int BegYear =0, BegMonth = 0, BegDay = 0, BegHourTime = 0, BegMinTime = 0;
            BegYear = int.Parse(DateTime.Parse(selectedDate.Text).Year.ToString());
            BegMonth = int.Parse(DateTime.Parse(selectedDate.Text).Month.ToString());
            BegDay = int.Parse(DateTime.Parse(selectedDate.Text).Day.ToString());
            BegHourTime = int.Parse(startHourCombo.SelectedItem.ToString());
            BegMinTime = int.Parse(startMinCombo.SelectedItem.ToString());

            String noonIndicator = strNoonIndicatorCombo.SelectedItem.ToString();

            if ((BegHourTime < 12) && noonIndicator == "PM")
            {
                BegHourTime = BegHourTime + 12;
            }
            else if((BegHourTime == 12) && (noonIndicator == "AM"))
            {
                BegHourTime = 0;
            }
            DateTime startDateTime = new DateTime(BegYear, BegMonth, BegDay, BegHourTime, BegMinTime, 0);
            return startDateTime;            
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get the selected end Date, start hours, start mins and start secs 
        /// from different places on the form and will return a DateTime object
        /// -------------------------------------------------------------------------------
        /// </summary>
        private DateTime GetSelectedEndDate()
        {
            int BegYear = 0, BegMonth = 0, BegDay = 0, EndHourTime = 0, EndMinTime = 0;
            BegYear = int.Parse(DateTime.Parse(selectedDate.Text).Year.ToString());
            BegMonth = int.Parse(DateTime.Parse(selectedDate.Text).Month.ToString());
            BegDay = int.Parse(DateTime.Parse(selectedDate.Text).Day.ToString());
            EndHourTime = int.Parse(endHourCombo.SelectedItem.ToString());
            EndMinTime = int.Parse(endMinCombo.SelectedItem.ToString());
            
            String noonIndicator = endNoonIndicatorCombo.SelectedItem.ToString();

            if ((EndHourTime < 12) && noonIndicator == "PM")
            {
                EndHourTime = EndHourTime + 12;
            }
            else if ((EndHourTime == 12) && (noonIndicator == "AM"))
            {
                EndHourTime = 0;
            }

            DateTime endDateTime = new DateTime(BegYear, BegMonth, BegDay, EndHourTime, EndMinTime, 0);
            return endDateTime;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get called when User selects HCW for creating Appointment
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void homecareWorkerGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //Enable User to Save Schedule
            int columnIndex = homecareWorkerGridView.CurrentCell.ColumnIndex;

            if (columnIndex == 0)
            {
                saveSchedule.Enabled = true;

                int UserSelectedRow = homecareWorkerGridView.SelectedCells[0].RowIndex;

                for (int iCntr = 0; iCntr < homecareWorkerGridView.Rows.Count; iCntr++)
                {
                    if (iCntr != UserSelectedRow)
                    {
                        if (homecareWorkerGridView.Rows[iCntr].Cells[0].Displayed.Equals(true))
                        {
                            homecareWorkerGridView.Rows[iCntr].Cells[0].Value = false;
                        }
                    }
                }
            }
            else if (columnIndex == 4)
            {
                int selectedRow = homecareWorkerGridView.SelectedCells[0].RowIndex;
                long hcwID = (long)homecareWorkerGridView.Rows[selectedRow].Cells[1].Value;
                FrmViewHCWScheduleProfile frm = new FrmViewHCWScheduleProfile(hcwID);
                frm.Show();
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get all the Appointment Information and call Save Appointment
        /// function to save the created Appoitnment
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void SaveSchedule_Click(object sender, EventArgs e)
        {
            //getting the client & hcw IDs and the appointment date and time
            int selectedRow = homecareWorkerGridView.SelectedCells[0].RowIndex;
            object hcwID = homecareWorkerGridView.Rows[selectedRow].Cells[1].Value;
            DateTime startDate = GetSelectedStartDate();
            DateTime endDate = GetSelectedEndDate();
            
            //if Saved, Save Schedule Button is disabled
            if (true == Library.Appointment.SaveAppointment(mClientID, (long)hcwID, startDate, endDate))
            {
                messageDisplayTextBox.Text = "Appointment is Saved.";
                messageDisplayTextBox.Visible = true;
                saveSchedule.Enabled = false;
            }
            else
            {
                messageDisplayTextBox.Text = "Appointment is failed to Saved. Please try again.";
                messageDisplayTextBox.Visible = true;
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function ensures that when the user clicks clear all the contents of the form are
        /// reloaded.
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void clearButton_Click(object sender, EventArgs e)
        {
            homecareWorkerGridView.Rows.Clear();
            selectedTasksBox.Items.Clear();
            taskList.Items.Clear();
            DisplayTaskList();
            availDetailsGrid.ClearSelection();
            for (int iCntr = 0; iCntr < availDetailsGrid.RowCount; iCntr++)
            {
                availDetailsGrid.Rows[iCntr].Cells[0].Value = false;
            }

            hcwSearchDone = false;

            messageDisplayTextBox.Visible = false;
            messageDisplayTextBox.Text = " ";
            startHourCombo.Enabled = true;
            startMinCombo.Enabled = true;
            strNoonIndicatorCombo.Enabled = true;
            endHourCombo.Enabled = true;
            endMinCombo.Enabled = true;
            endNoonIndicatorCombo.Enabled = true;

            selectedDate.Text = "";

            startHourCombo.SelectedItem = " ";
            startMinCombo.SelectedItem = " ";
            strNoonIndicatorCombo.SelectedItem = " ";
            endHourCombo.SelectedItem = " ";
            endMinCombo.SelectedItem = " ";
            endNoonIndicatorCombo.SelectedItem = " ";

            saveSchedule.Enabled = false;

            availDetailsGrid.Columns[0].ReadOnly = false;

            weekPicker.Enabled = true;

            hcwSearchButton.Enabled = true;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Makes sure that a past date is not selected for scheduling an relaod the availability time details
        /// when the week is changed. Also reloads the new 'Remaining hours'
        /// -------------------------------------------------------------------------------
        /// </summary>
        private void weekPicker_ValueChanged(object sender, EventArgs e)
        {
            DateTime today = new DateTime();
            today = DateTime.Today;
            if (weekPicker.Value < today)
            {
                weekPicker.Value = today;
            }

            DisplayClientRemainingHours();
            DisplayTimeSlots();
        }

        //-------------------------------------------------------------------------------
        //Private Data Members
        //-------------------------------------------------------------------------------
        private long mClientID;
        private Boolean hcwSearchDone = false;

    }
}